package com.witdata.common.service.community.impl;

import com.mybatisflex.core.query.QueryWrapper;
import com.witdata.mysql.base.service.CommunityBaseService;
import com.witdata.common.domain.Status;
import com.witdata.common.domain.dto.CommunityDto;
import com.witdata.common.domain.dto.CommunityMemberDto;
import com.witdata.mysql.domain.entity.TCommunity;
import com.witdata.common.exception.GlobalException;
import com.witdata.common.mapper.CommunityMapper;
import com.witdata.common.service.FileService;
import com.witdata.common.service.community.CommunityService;
import com.witdata.common.service.community.CommunityTagService;
import com.witdata.common.service.community.MemberService;
import com.witdata.common.util.IdUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service
@RequiredArgsConstructor
public class CommunityServiceImpl implements CommunityService {
    private final CommunityBaseService communityBaseService;
    private final CommunityTagService communityTagService;
    private final MemberService memberService;
    private final FileService fileService;
    private final IdUtils idUtils;

    @Override
    public List<CommunityDto> getCommunityList() {
        List<TCommunity> tCommunityList = communityBaseService.list();
        return CommunityMapper.INSTANCE.toCommunityDtoList(tCommunityList);
    }

    @Override
    public CommunityDto getCommunityById(Long id) {
        TCommunity tCommunity = communityBaseService.getById(id);
        return CommunityMapper.INSTANCE.toCommunityDto(tCommunity);
    }

    @Override
    public List<CommunityDto> getCommunityListByUserId(Long userId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("creator", userId);
        List<TCommunity> tCommunityList = communityBaseService.list(queryWrapper);
        return CommunityMapper.INSTANCE.toCommunityDtoList(tCommunityList);
    }

    @Override
    @Transactional
    public void createCommunity(MultipartFile avatar, String name, String description, String title) {
        Long id = IdUtils.createId();
        Long userId = idUtils.getUserIdByRequest();
        String uploaded = fileService.uploadFile(avatar, "/community/" + id);
        TCommunity community = TCommunity.builder()
                .id(id)
                .name(name)
                .url(uploaded)
                .description(description)
                .title(title)
                .creator(userId)
                .delFlag(false)
                .build();
        boolean save = communityBaseService.save(community);
        if (!save) throw new GlobalException(Status.CREATEERROR);
        memberService.addMember(id, userId);
        communityTagService.addDefaultTag(id);
    }

    @Override
    @Transactional
    public void dissolveCommunity(CommunityDto communityDto) {
        Long id = communityDto.getId();
        Long userId = idUtils.getUserIdByRequest();
        boolean remove = communityBaseService.remove(new QueryWrapper().eq("id", id));
        if (!remove) throw new GlobalException(Status.DissolveError);
        memberService.leaveCommunity(CommunityMemberDto.builder().communityId(id).userId(userId).build());
    }
}
